package com.chance.cc.crawler.development.command.job.domain.weibo;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.downloader.proxy.Proxy;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.record.CrawlerRecord;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.development.command.job.CrawlerJobCommand;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import org.apache.commons.lang3.StringUtils;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRecordFilter.dateRange;
import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.*;
import static com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime;

/**
 * @Author Zhao.Hhuan
 * @Date Create in 2020/12/30 13:34
 * @Description
 *      微博关键词
 **/
public class WeiboSearchKwCrawlerScheduleJob {

    private static final String searchUrl = "https://s.weibo.com/?Refer=";

    public static final String domainId = "weibo";
    public static final String site = "searchKw";

    private static Proxy proxy = new Proxy();
    static {
        //代理配置
        proxy.setHost("http-dyn.abuyun.com");
        proxy.setPort(9020);
        proxy.setUsername("HEW657EL99F83S9D");
        proxy.setPassword("8916B1F3F10B1979");
    }
    private static CrawlerJobCommand crawlerJobCommand = new CrawlerJobCommand("192.168.1.215",9599);


    public static void main(String[] args) {
        publishCrawlerScheduleJobInfo();
    }

    public static CrawlerJob publishCrawlerScheduleJobInfo(){


        CrawlerJob weiboSchdule = weiboCrawlerSchdule();

        //发布定时采集作业
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domainId);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(weiboSchdule));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("微博自主开发关键词定时采集");
        crawlerScheduleJob.setCrawlerKey(weiboSchdule.generateCrawlerKey());
        HttpPage page = crawlerJobCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布作业：" + page.getRawText());
        return weiboSchdule;

    }

    public static CrawlerJob weiboCrawlerSchdule() {
        String siteBiz = "self_searchKw-realtime";
        CrawlerRequestRecord requestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domainId, turnPageItem)
                .httpUrl(searchUrl)
                .releaseTime(System.currentTimeMillis())
                .filter(CrawlerEnum.CrawlerRecordFilter.keyOrDateRange)
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(24 * 1,null))
                .addFilterInfo(FilterUtils.redisFilterKeyInfo(StringUtils.joinWith("-","filter",domainId,site,siteBiz,"queue")))
                .proxy(proxy)
                .needWashed(false)
                .needParsed(false)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.article)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.interaction)
                .build();
        requestRecord.setDownload(false);
        requestRecord.setSkipPipeline(true);
        requestRecord.tagsCreator().bizTags().addDomain(domainId);
        requestRecord.tagsCreator().bizTags().addSite(site);
        requestRecord.tagsCreator().bizTags().addSiteBiz(siteBiz);

        CrawlerRecord commentFilter = new CrawlerRequestRecord();
        commentFilter.setFilter(dateRange);
//        commentFilter.addFilterInfo(FilterUtils.memoryFilterKeyInfo(StringUtils.joinWith("-","filter",domainId,site,"queue")));
        commentFilter.addFilterInfo(FilterUtils.dateRangeFilterInfo(24,null));
        requestRecord.tagsCreator().resultTags().getCategoryTag().addKVTag("comment_filter_record", JSON.toJSONString(commentFilter));
        requestRecord.getHttpRequest().addExtra("searchKwSourceUrl","https://s.weibo.com/weibo?q=%s&typeall=1&suball=1&timescope=custom:2020-12-01:&Refer=g");

        //keyword record
        CrawlerRequestRecord keywordRecord = CrawlerRequestRecord.builder()
                .startPageRequest("weibo_search_kw",turnPageItem)
                .httpUrl("http://192.168.1.215:9599/v1/meta/"+domainId+"/keys?site=" + site)
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();

        //user oauth info source
        CrawlerRequestRecord userOauthInfoCrawlerRecord = CrawlerRequestRecord.builder()
                .startPageRequest("weibo_user_oauth_infos",turnPageItem)
                .httpUrl("http://192.168.1.215:9599/crawler/oauth/api/v1/"+domainId+"/userOauthInfos")
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();

        //user agents
        CrawlerRequestRecord userAgentsCrawlerRecord = CrawlerRequestRecord.builder()
                .startPageRequest("weibo_user_agents", turnPageItem)
                .httpUrl("https://fake-useragent.herokuapp.com/browsers/0.1.11")
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();


        return CrawlerJob.builder()
                .crawlerJobThreadNumber(3)
                .triggerInfo(
                        domainId,
                        CrawlerMetaConstant.ScheduleJobTrigger_Cron,
                        System.currentTimeMillis(),
                        StringUtils.joinWith("-",domainId,site,siteBiz,ScheduleJobTriggerJob_Realtime))
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(StringUtils.joinWith("-","crawler",domainId,site,siteBiz,"queue")))
//                .fileResultPipeline(null, "/data/chance_crawler_test/data/weibo/weibo_self_searchKw.log", false)
                .kafkaResultPipeline(null,"tmp_social_media_weibo",null)
                .requestRecord(requestRecord)
                .supportRecord(keywordRecord)
                .supportRecord(userAgentsCrawlerRecord)
                .supportRecord(userOauthInfoCrawlerRecord)
                .build();
    }
}
